package org.common.utils.fasttest;

import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author zhouzhibing
 * @date 2024/3/22
 */
@Slf4j
public class TestList {
    public static void testList() {
        int count = 10000;
        int round = 10;

//        CopyOnWriteArrayList
        List<Double> list = Collections.synchronizedList(new ArrayList<>());
//        List<Double> list = new CopyOnWriteArrayList<>();

        AtomicLong atomicSize = new AtomicLong();

        Test.doHandler("List.runAsync.add " , count , round,  (o) -> {
//            CompletableFuture.runAsync(() ->{
//                list.add(ThreadLocalRandom.current().nextDouble());
//                atomicSize.incrementAndGet();
//            });

            list.add(ThreadLocalRandom.current().nextDouble());
//            atomicSize.incrementAndGet();
        });

//        waitCountFinish("test" , atomicSize , (count * round));
        System.err.println(list.size());

//        Test.doHandler("javaList.add " , 1, round ,  (o) -> {
//            List<Integer> newList = new ArrayList<>(count);
//            for (Integer i : list) {
//                newList.add(i);
//            }
//        });

//        Test.doHandler("javaList.addAll " , 1, round ,  (o) -> {
//            List<Integer> newList = new ArrayList<>(count);
//            newList.addAll(list);
//        });
//
//        Test.doHandler("javaList.new " , 1, round ,  (o) -> {
//            List<Integer> newList = new ArrayList<>(list);
//        });


//        Test.doHandler("javaList.add " , 1, round ,  () -> {
//            List<Integer> javaList = new ArrayList<>();
//            for (int i = 0; i < count; i++) {
//                javaList.add(i);
//            }
//        });

//        Test.doHandler("trove intList.add " , 1, round , () -> {
//            TIntList intList  = new TIntArrayList();
//            for (int i = 0; i < count; i++) {
//                intList.add(i);
//            }
//        });

//        Test.doHandler("eclipse intList.add " , 1, round , () -> {
//            MutableIntList intList = new org.eclipse.collections.impl.list.mutable.primitive.IntArrayList(10);
//            for (int i = 0; i < count; i++) {
//                intList.add(i);
//            }
//        });
//
//
//        Test.doHandler("fastutil intList.add " , 1, round , (index) -> {
//            IntList intList = new IntArrayList(10);
//            for (int i = 0; i < count; i++) {
//                intList.add(i);
//            }
//        });


//        Int2ObjectMap map = new Int2ObjectOpenHashMap();
    }




    private static void waitCountFinish(String name , AtomicLong atomicLong , long waitCount) {
        long loopCount = 0;
        try {
            while(atomicLong.get() < waitCount) {
                Thread.sleep(10);
                loopCount ++;
                log.info("BaphometTurnTask.waitCountFinish while name = {} , loopCount = {} , atomicLong = {} , waitCount = {} " , name , loopCount , atomicLong , waitCount);
            }
            log.info("BaphometTurnTask.waitCountFinish finish name = {} , loopCount = {} , atomicLong = {} , waitCount = {} " , name , loopCount , atomicLong , waitCount);
        } catch (InterruptedException e) {
            log.error("BaphometTurnTask.waitCountFinish error name = {} , loopCount = {} , atomicLong = {} , waitCount = {} " , name , loopCount , atomicLong , waitCount , e);
            throw new RuntimeException(e);
        }
    }
}
